package com.dream52.pms.common;

import java.util.Enumeration;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;

public class Request {

	private HttpServletRequest request = null;

	public Request(HttpServletRequest request1) {
		request = request1;
	}

	public void setRequest(HttpServletRequest request1) {
		request = request1;
	}

	public HashMap<String, String> getFormMap() {
		HashMap<String, String> map = getFormMap(request);
		return map;
	}

	public static HashMap<String, String> getFormMap(HttpServletRequest request1) {
		HashMap<String, String> map = new HashMap<String, String>();
		String name;
		for (Enumeration<String> names = request1.getParameterNames(); names
				.hasMoreElements(); map.put(name, request1.getParameter(name)))
			name = (String) names.nextElement();
		return map;
	}
	
	public double getDouble(String paraname) {
		return getDouble(paraname, 0);
	}
	
	public double getDouble(String paraname, double defaultvalue) {
		double rst = defaultvalue;
		if (request.getParameter(paraname) != null)
			rst = Double.parseDouble(request.getParameter(paraname));
		else if (request.getAttribute(paraname) != null)
			rst = Double.parseDouble(request.getAttribute(paraname).toString());
		return rst;
	}
	
	public int getInt(String paraname) {
		return getInt(paraname, 0);
	}

	public int getInt(String paraname, int defaultvalue) {
		int rst = defaultvalue;
		if (request.getParameter(paraname) != null && !request.getParameter(paraname).equals(""))
			rst = Integer.parseInt(request.getParameter(paraname));
		else if (request.getAttribute(paraname) != null && !request.getAttribute(paraname).equals(""))
			rst = Integer.parseInt(request.getAttribute(paraname).toString());
		return rst;
	}

	public long getLong(String paraname) {
		return getLong(paraname, 0);
	}

	public long getLong(String paraname, long defaultvalue) {
		long rst = defaultvalue;
		if (request.getParameter(paraname) != null)
			rst = Long.parseLong(request.getParameter(paraname));
		else if (request.getAttribute(paraname) != null)
			rst = Long.parseLong(request.getAttribute(paraname).toString());
		return rst;
	}

	public String getString(String paraname) {
		return getString(paraname, "");
	}

	public String getString(String paraname, String defaultvalue) {
		String rst = defaultvalue;
		if (request.getParameter(paraname) != null)
			rst = request.getParameter(paraname);
		else if (request.getAttribute(paraname) != null)
			rst = request.getAttribute(paraname).toString();
		return rst;
	}

	public Boolean getBoolean(String paraname) {
		return getBoolean(paraname, false);
	}

	public Boolean getBoolean(String paraname, Boolean defaultvalue) {
		Boolean rst = defaultvalue;
		if (request.getParameter(paraname) != null)
			rst = Boolean.valueOf(request.getParameter(paraname));
		else if (request.getAttribute(paraname) != null)
			rst = Boolean.valueOf(request.getAttribute(paraname).toString());
		return rst;
	}

	public String getRemoteAddr() {
		return request.getRemoteAddr();
	}

	public String getForwardedAddr() {
		if (request.getHeader("HTTP_X_FORWARDED_FOR") != null)
			return request.getHeader("HTTP_X_FORWARDED_FOR");
		else
			return "";
	}

	public static String getRemoteAddr(HttpServletRequest request1) {
		return request1.getRemoteAddr();
	}

	public String getRemoteHost() {
		return request.getRemoteHost();
	}

	public static String getRemoteHost(HttpServletRequest request1) {
		return request1.getRemoteHost();
	}

	public String getRemoteUser() {
		return request.getRemoteUser();
	}

	public static String getRemoteUser(HttpServletRequest request1) {
		return request1.getRemoteUser();
	}

	public static String getUrl(HttpServletRequest request1) {
		return request1.getServletPath();
	}

	public String getUrl() {
		return request.getServletPath();
	}

	public static String getServerName(HttpServletRequest request1) {
		return request1.getServerName();
	}

	public String getServerName() {
		return request.getServerName();
	}

	public static String getProtocol(HttpServletRequest request1) {
		return request1.getProtocol();
	}

	public String getProtocol() {
		return request.getProtocol();
	}

	public String getHttp() {
		String path = request.getProtocol();
		if (path.startsWith("HTTPS"))
			return "HTTPS";
		if (path.startsWith("FTP"))
			return "FTP";
		if (path.startsWith("HTTP"))
			return "HTTP";
		else
			return "HTTP";
	}

	public static String getHttp(HttpServletRequest request1) {
		String path = request1.getProtocol();
		if (path.startsWith("HTTPS"))
			return "HTTPS";
		if (path.startsWith("FTP"))
			return "FTP";
		if (path.startsWith("HTTP"))
			return "HTTP";
		else
			return "HTTP";
	}

	public static String getDomain(HttpServletRequest request1) {
		return (new StringBuilder(String.valueOf(getHttp(request1)))).append(
				"://").append(getServerName(request1)).toString();
	}

	public String getDomain() {
		return (new StringBuilder(String.valueOf(getHttp(request)))).append(
				"://").append(getServerName(request)).toString();
	}

	public String getReferer() {
		return request.getHeader("Referer");
	}

	public String getBasePath() {
		int port = request.getServerPort();
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://" + request.getServerName()
				+ path;
		if (port != 80) {
			basePath = request.getScheme() + "://" + request.getServerName()
					+ ":" + port + path;
		}
		return basePath;
	}

}
